﻿/******************************************************************************************
*                                                                                         *    
*                              Developed by Bruno Pires                                   *
*                                                                                         *
*                             email: bruno@blastersystems.com                             *
*                               web: www.blastersystems.com                               *
*                              blog: www.blastersystems.com/blog                          *
*                           twitter: brunoacpires                                         *
*                                                                                         *
*                      Software available under GNU LGPL License                          *
*                                                                                         *    
* *****************************************************************************************/



using System;
using System.Collections.Generic;
using System.Web.UI; 
using System.Web.UI.WebControls;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;

namespace AspNetSocialBookmarks
{
    
    public partial class Bookmarks : System.Web.UI.UserControl
    {
        #region Emumerators

        public enum myBookmark
        {
            email,
            facebook,
            feed,
            flickr,
            googleBookmarks,
            linkedin,
            map,
            picasa,
            twitter
        };

        public enum myBookmarkStyle
        {
            small,  /*25px*/
            medium, /*35px*/
        };

        #endregion

        #region Properties

        public string MyTitle                       { get; set; }
        //public int MyControlWidth                   { get; set; } /*ToDo*/
        //public int NumberOfBookmarks                { get; set; } /*ToDo*/
        public myBookmarkStyle BookmarkSize         { get; set; }

        private Dictionary<myBookmark, bool>        EstadoControlos;
        
        public bool ShowEmail                       { get; set; }
        public bool ShowFacebook                    { get; set; }
        public bool ShowFeeds                       { get; set; }
        public bool ShowFlickr                      { get; set; }
        public bool ShowGoogleBookmarks             { get; set; }
        public bool ShowLinkedin                    { get; set; }
        public bool ShowMap                         { get; set; }
        public bool ShowPicasa                      { get; set; }
        public bool ShowTwitter                     { get; set; }
        public bool ShowTitle                       { get; set; }

        public string EmailUrl                      { get; set; }
        public string EmailAddress                  { get; set; }
        public string EmailSubject                  { get; set; }
        public string FeedsUrl                      { get; set; }
        public string FlickrUrl                     { get; set; }
        public string MapUrl                        { get; set; }
        public string PicasaUser                    { get; set; }
        public string PicasaAlbum                   { get; set; }
        public string TwitterUser                   { get; set; }
        public string TwitterUrlToShare             { get; set; }
        public string TweetToShare                  { get; set; }
        public string GoogleBookmarksUrl            { get; set; }
        public string GoogleBookmarksTitle          { get; set; }
        public string FacebookUrl2Share             { get; set; }
        public string FacebookTitle2Share           { get; set; }
        public string LinkedinUrl2Share             { get; set; }
        public string LinkedinPageTitle2Share       { get; set; }
        public string LinkedinSource2Share          { get; set; }
        public string LinkedinSummary2Share         { get; set; }

        public string EmailTooltip                  { get; set; }
        public string FacebookTooltip               { get; set; }
        public string FeedsTooltip                  { get; set; }
        public string FlickrTooltip                 { get; set; }
        public string GoogleBookmarksUrlTooltip     { get; set; }
        public string LinkedinTooltip               { get; set; }
        public string MapTooltip                    { get; set; }
        public string PicasaTooltip                 { get; set; }
        public string TwitterTooltip                { get; set; }

        #endregion

        #region Main Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ShowTitle)
                AddTitleToBookmark(MyTitle);
            EstadoControlos = new Dictionary<myBookmark, bool>();
            LoadControlsState();
            RenderMyControls();
        }

        public void AddSocialBookmark(myBookmark bookmarkType)
        {          
            switch (bookmarkType)
            {
                case myBookmark.email:
                    var BtnEmail = new ImageButton();
                    BtnEmail.ID = "BtnEmail";
                    BtnEmail.Enabled = true;
                    BtnEmail.ToolTip = EmailTooltip;
                    BtnEmail.Attributes.Add("onclick", "openPage('" + GetEmailUrl(EmailAddress,EmailUrl,EmailSubject) + "')");
                    BtnEmail.ImageUrl="~/icons/ico-email.png";
                    BtnEmail.CssClass = "bookmark-btn-small";
                    BtnEmail.Visible=true;
                    AddBookmarkToHtml(DivBtn,BtnEmail);
                    break;
                case myBookmark.facebook:
                    var BtnFacebook = new ImageButton();
                    BtnFacebook.ID = "BtnFacebook";
                    BtnFacebook.Enabled = true;
                    BtnFacebook.ToolTip = FacebookTooltip;
                    BtnFacebook.Attributes.Add("onclick", "openPage('" + GetFacebookUrl(FacebookUrl2Share, FacebookTitle2Share) + "')");
                    BtnFacebook.ImageUrl = "~/icons/facebook.png";
                    BtnFacebook.CssClass = "bookmark-btn-small";
                    BtnFacebook.EnableViewState = false;
                    AddBookmarkToHtml(DivBtn, BtnFacebook);
                    break;
                case myBookmark.feed:
                    var BtnFeed = new ImageButton();
                    BtnFeed.ID = "BtnFeed";
                    BtnFeed.Enabled = true;
                    BtnFeed.ToolTip = FeedsTooltip;
                    BtnFeed.Attributes.Add("onclick", "openPage('" + FeedsUrl + "')");
                    BtnFeed.ImageUrl = "~/icons/feed.png";
                    BtnFeed.CssClass = "bookmark-btn-small";
                    BtnFeed.EnableViewState = false;
                    AddBookmarkToHtml(DivBtn, BtnFeed);
                    break;
                case myBookmark.flickr:
                    var BtnFlickr = new ImageButton();
                    BtnFlickr.ID = "BtnFlickr";
                    BtnFlickr.Enabled = true;
                    BtnFlickr.ToolTip = FlickrTooltip;
                    BtnFlickr.Attributes.Add("onclick", "openPage('" + FlickrUrl + "')");
                    BtnFlickr.ImageUrl = "~/icons/flickr.png";
                    BtnFlickr.CssClass = "bookmark-btn-small";
                    BtnFlickr.EnableViewState = false;
                    AddBookmarkToHtml(DivBtn, BtnFlickr);
                    break;
                case myBookmark.googleBookmarks:
                    var BtnGoogleBookmarks = new ImageButton();
                    BtnGoogleBookmarks.ID = "BtnGoogleBookmarks";
                    BtnGoogleBookmarks.Enabled = true;
                    BtnGoogleBookmarks.ToolTip = GoogleBookmarksUrlTooltip; ;
                    BtnGoogleBookmarks.Attributes.Add("onclick", "openPage('" + GetGoogleUrl(GoogleBookmarksUrl,GoogleBookmarksTitle) + "')");
                    BtnGoogleBookmarks.ImageUrl = "~/icons/google.png";
                    BtnGoogleBookmarks.CssClass = "bookmark-btn-small";
                    BtnGoogleBookmarks.EnableViewState = false;
                    AddBookmarkToHtml(DivBtn, BtnGoogleBookmarks);
                    break;
                case myBookmark.linkedin:
                    var BtnLinkedin = new ImageButton();
                    BtnLinkedin.ID = "BtnLinkedin";
                    BtnLinkedin.Enabled = true;
                    BtnLinkedin.ToolTip = LinkedinTooltip;
                    BtnLinkedin.Attributes.Add("onclick", "openPage('" + GetLinkedinUrl(LinkedinUrl2Share, LinkedinPageTitle2Share, LinkedinSource2Share, LinkedinSummary2Share) + "')");
                    BtnLinkedin.ImageUrl = "~/icons/linkedin.png";
                    BtnLinkedin.CssClass = "bookmark-btn-small";
                    BtnLinkedin.EnableViewState = false;                   
                    AddBookmarkToHtml(DivBtn, BtnLinkedin);
                    break;
                case myBookmark.map:
                    var BtnMap = new ImageButton();
                    BtnMap.ID = "BtnMap";
                    BtnMap.Enabled = true;
                    BtnMap.ToolTip = MapTooltip;
                    BtnMap.Attributes.Add("onclick", "openPage('" + MapUrl + "')");
                    BtnMap.ImageUrl = "~/icons/map-icon.jpg";
                    BtnMap.CssClass = "bookmark-btn-small";
                    BtnMap.EnableViewState = false;
                    AddBookmarkToHtml(DivBtn, BtnMap);
                    break;
                case myBookmark.picasa:
                    var BtnPicasa = new ImageButton();
                    BtnPicasa.ID = "BtnPicasa";
                    BtnPicasa.Enabled = true;
                    BtnPicasa.ToolTip = PicasaTooltip;
                    BtnPicasa.Attributes.Add("onclick", "openPage('" + GetPicasaUrl(PicasaUser, PicasaAlbum) + "')");
                    BtnPicasa.ImageUrl = "~/icons/picasa.png";
                    BtnPicasa.CssClass = "bookmark-btn-small";
                    BtnPicasa.EnableViewState = false;
                    AddBookmarkToHtml(DivBtn, BtnPicasa);
                    break;
                case myBookmark.twitter:
                    var BtnTwitter = new ImageButton();
                    BtnTwitter.ID="BtnTwitter";
                    BtnTwitter.ToolTip=TwitterTooltip;
                    BtnTwitter.Attributes.Add("onclick", "openPage('" + GetTwitterUrl(TwitterUrlToShare, TweetToShare, TwitterUser) + "')");
                    BtnTwitter.ImageUrl="~/icons/Twitter-icon.png";
                    BtnTwitter.CssClass="bookmark-btn-small";
                    BtnTwitter.EnableViewState=true;
                    AddBookmarkToHtml(DivBtn,BtnTwitter);
                    break;
                default:
                    break;
            }
        }

        public void AddTitleToBookmark(string text)
        {
            DivHeader.Visible = true;
            HeaderText.Visible = true;
            HeaderText.InnerText = text;
        }

        private void RenderMyControls()
        {
            foreach (KeyValuePair<myBookmark,bool> item in EstadoControlos)
            {
                if (item.Value)
                {
                    AddSocialBookmark(item.Key);
                }
            }
        }

        private void LoadControlsState()
        {
            EstadoControlos.Add(myBookmark.email, ShowEmail);
            EstadoControlos.Add(myBookmark.facebook, ShowFacebook);
            EstadoControlos.Add(myBookmark.feed, ShowFeeds);
            EstadoControlos.Add(myBookmark.flickr, ShowFlickr);
            EstadoControlos.Add(myBookmark.googleBookmarks, ShowGoogleBookmarks);
            EstadoControlos.Add(myBookmark.linkedin, ShowLinkedin);
            EstadoControlos.Add(myBookmark.map, ShowMap);
            EstadoControlos.Add(myBookmark.picasa, ShowPicasa);
            EstadoControlos.Add(myBookmark.twitter, ShowTwitter);
        }

        private void AddBookmarkToHtml(HtmlGenericControl divToPublish, ImageButton btn)
        {
             divToPublish.Controls.Add(btn as Control);
        }

        #endregion

        #region Get Methods

        public string GetEmailUrl(string mailto, string serviceUrl, string subject)
        {
            StringBuilder url = new StringBuilder(string.Format("mailto:{0}?body=Encontrei um artigo interessante: {1}&subject={2}",
               mailto, serviceUrl, subject));
            return url.ToString();
            
        }

        public string GetFacebookUrl(string serviceUrl, string pageTitle)
        {
            StringBuilder url = new StringBuilder(string.Format("http://www.facebook.com/sharer.php?u={0}&t={1}",
                GetShortUrl(serviceUrl), pageTitle));
            return url.ToString();

        }

        public string GetFeedUrl(string serviceUrl, string pageTitle)
        {
            return null;
        }

        public string GetFlickrUrl(string serviceUrl, string album)
        {
            return null;
        }

        public string GetGoogleUrl(string serviceUrl, string pageTitle)
        {
            StringBuilder url = new StringBuilder(string.Format("http://google.pt/bookmarks/mark?op=edit&bkmk={0}&title={1}",
                GetShortUrl(serviceUrl), pageTitle));
            return url.ToString();
        }

        public string GetLinkedinUrl(string urlToShare,string pageTitle, string source, string summary)
        {
            StringBuilder url = new StringBuilder(string.Format("http://www.linkedin.com/shareArticle?mini=true&url={0}&title={1}",
                GetShortUrl(urlToShare), pageTitle));

            if (summary != null)
                url.Append(string.Format("&summary={0}", summary));
            if (source != null)
                url.Append(string.Format("&source={0}", source));

            return url.ToString();

        }

        public string GetMapUrl(string serviceUrl, string pageTitle)
        {
            return null;
        }

        public string GetPicasaUrl(string user, string album)
        {
            StringBuilder url = new StringBuilder(string.Format("http://picasaweb.google.com/{0}/{1}",
                user,album));
            return url.ToString(); 
        }

        public string GetTwitterUrl(string urlToShare, string tweet, string twitterUser)
        {
            StringBuilder url = new StringBuilder(string.Format("http://twitter.com/home?status={0} {1}",
                GetShortUrl(urlToShare), tweet));

            if(twitterUser!=string.Empty)
                url.Append(string.Format(" - via(@{0})",twitterUser));

            return url.ToString(); 
        }

        public string GetShortUrl(string urlToShorten)
        {
            try
            {
                if (!urlToShorten.ToLower().StartsWith("http") && !urlToShorten.ToLower().StartsWith("ftp"))
                {
                    urlToShorten = "http://" + urlToShorten;
                }
                var request = WebRequest.Create("http://tinyurl.com/api-create.php?url=" + urlToShorten);
                var res = request.GetResponse();
                string shortUrl;
                using (var reader = new StreamReader(res.GetResponseStream()))
                {
                    shortUrl = reader.ReadToEnd();
                }
                return shortUrl;
            }
            catch (Exception)
            {
                return urlToShorten;
            }
            
        }
        
        #endregion
    }
}